<script setup lang="ts">
import { deleteReportRecord } from "@/api/report";
import { onMounted, reactive, toRefs, ref, computed, nextTick } from "vue";
import { PxMessage, PxMessageBox } from "px-ui";
import { useRouter, useRoute } from "vue-router";
import { useNav } from "@/layout/hooks/useNav";
import dayjs from "dayjs";

defineOptions({
  name: "acidBarrelsCount"
});
const { username } = useNav();
const router = useRouter();
const route = useRoute();

const templateUrl = ref();

const queryParams = reactive({
  endDate: ""
});

const submitAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().verifyAndWriteReport()", "*");
};
// importExcel_Clean清空导入
// importExcel_Append 增量导入
// importExcel_Cover 覆盖导入
const importAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().importExcel_Cover()", "*");
};
const exportAction = () => {
  document.getElementById("reportFrame").contentWindow.postMessage("_g().exportReportToExcel('simple')", "*");
};
const deleteAction = () => {
  PxMessageBox.confirm(`是否该条记录数据？`, "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true
  })
    .then(async () => {
      await deleteReportRecord({ ids: [route.query.id] });
      PxMessage.success("删除成功");
      templateUrl.value = `${window.location.protocol}//${window.location.hostname}:8443/webroot/decision/view/report?viewlet=${route.query.template}&user=${username.value}&__pi__=false&facCode=${localStorage.getItem("facCode") || ""}`;
      localStorage.setItem("listen_report_record_delete", "true");
      setTimeout(() => {
        window.close();
      }, 1500);
    })
    .catch(() => {});
  //window.close()
};
const printAction = () => {
  // document.getElementById('reportFrame').contentWindow.contentPane.noClientPrint(true, true)();
  // let config = JSON.stringify({
  //   printUrl: `${VITE_FANRUAN_BASE_URL}/webroot/decision/view/report`,
  //   printType: 0, // 1
  //   isPopUp: true,
  //   pageType: 0,
  //   data: {
  //     reportlets:[{reportlet: `${decodeURIComponent(decodeURIComponent(route.query.template))}`, cmd: 'save_w_content', op: 'write', singleCode: route.query.singleCode}]
  //   }
  // })
  // // console.log('config:' + JSON.stringify(config));
  // // let config = JSON.stringify({ url: templateUrl.value, isPopUp: true });
  // //doURLPrint doURLPDFPrint
  // document.getElementById("reportFrame").contentWindow.postMessage(`FR.doURLPrint(${config})`, "*");
  document.getElementById("reportFrame").contentWindow.postMessage(`_g().noClientPrint(true,true)`, "*");
};
const { VITE_FANRUAN_BASE_URL } = import.meta.env;
const getUrl = () => {
  let tpName = encodeURIComponent(encodeURIComponent("reportCenter/gas/点酸桶数.cpt"));
  let baseUrl = `${VITE_FANRUAN_BASE_URL}/webroot/decision/view/report?viewlet=${tpName}&user=${username.value}&__pi__=false&op=write&facCode=${localStorage.getItem("facCode") || ""}`;
  let endDate = queryParams?.endDate || "";
  let startDate = dayjs(endDate).subtract(7, "day").format("YYYY-MM-DD") || "";
  if (startDate && endDate) {
    baseUrl = `${baseUrl}&startDate=${startDate}&endDate=${endDate}`;
  }
  templateUrl.value = baseUrl;
};
onMounted(() => {
  let ym = new Date();
  if (route.query.date) {
    ym = new Date(route.query.date);
  }
  // 获取当前月份第一天
  const firstDayOfMonth = dayjs(new Date()).subtract(7, "day").format("YYYY-MM-DD");
  // 获取当前月份最后一天
  const lastDayOfMonth = dayjs(new Date()).format("YYYY-MM-DD");
  queryParams.endDate = lastDayOfMonth;
  getUrl();
});

const searchAction = () => {
  getUrl();
};
</script>

<template>
  <div class="">
    <px-form ref="queryRef" class="custom-form-ui" :model="queryParams" inline label-width="90px">
      <px-form-item label="时间" prop="timeRange" style="width: 250px">
        <px-date-picker v-model="queryParams.endDate" type="date" placeholder="请选择时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </px-form-item>
      <px-form-item class="option-item">
        <px-button type="primary" @click="searchAction">查询</px-button>
        <px-button v-has="'GAS_OPERATION_ACIDDRUMMANAGE_POINT_EXPORT'" type="primary" plain @click="exportAction">导出</px-button>
        <!-- <px-button @click="resetQuery">重置</px-button> -->
      </px-form-item>
    </px-form>
    <div style="position: relative; height: calc(100vh - 340px); margin: 16px 0; overflow-y: hidden; background: #fff">
      <iframe id="reportFrame" :src="templateUrl" width="100%" height="100%" frameborder="0" />
    </div>
    <div class="op-btns footer-btns">
      <div class="right"><px-button v-has="'GAS_OPERATION_ACIDDRUMMANAGE_POINT_SUBMIT'" type="primary" @click="submitAction">提交</px-button></div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.template-content {
  width: 100%;
  background-color: #fff;
}

.custom-form-ui {
  height: 38px;

  .px-form-item {
    margin-bottom: 0;
  }
}

.op-btns {
  display: flex;
  padding: 16px;
  margin: 5px 0;
  background-color: #fff;
  box-shadow: 0 0 10px 0 rgb(0 0 0 / 10%);

  .left {
    flex: 1;
    text-align: left;
  }

  .center {
    flex: 1;
    text-align: center;
  }

  .right {
    flex: 1;
    text-align: right;
  }
}

.footer-btns {
  text-align: right;
}
</style>
